package cn.gov.anhai.service;

import cn.gov.anhai.mappers.FolkloreIntangibleHeritageMapper;
import cn.gov.anhai.mappers.PublicMapper;
import cn.gov.anhai.mappers.RuralCulturalTourismMapper;
import cn.gov.anhai.utils.MyUtil;
import cn.gov.anhai.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 民俗非遗
 */
@Service
public class FolkloreIntangibleHeritageService {
    @Resource
    FolkloreIntangibleHeritageMapper folkloreIntangibleHeritageMapper;

    @Resource
    PublicMapper publicMapper;

    @Resource
    PublicService publicService;

    /**
     * 乡村文旅列表
     */
    public List<Map<String, Object>> list(Integer current, Integer size) {
        PageUtil pageUtil = new PageUtil(current, size);
        List<Map<String, Object>> list = folkloreIntangibleHeritageMapper.list(pageUtil.getOffset(), pageUtil.getRow());
        list.forEach(item -> {
            if (!MyUtil.hasMapKey(item, "view")) item.put("view", 0);
            item.put("desc", MyUtil.getTextByHtml((String) item.get("desc")));
        });
        return list;
    }

    /**
     * 乡村文旅列表（个数）
     */
    public Integer listCount() {
        Integer count = folkloreIntangibleHeritageMapper.listCount();
        return count;
    }

    /**
     * 根据编号查询乡村文旅信息
     */
    public Map<String, Object> info(Long id){
        Map<String, Object> info = folkloreIntangibleHeritageMapper.info(id);
        if (info == null) return null;
        folkloreIntangibleHeritageMapper.updateCount(id);
        return info;
    }

    /**
     * 民俗非遗背景
     */
    public String back() {
        String data = publicMapper.getConfigByKey("folkway_background");
        if (data == null) return null;
        try {
            long id = Long.parseLong(data);
            return publicService.getOSSUrl(id);
        } catch (Exception ex) {
            return null;
        }
    }

}
